package com.kingmed.kmss.bpmn.flow.listener;

import java.util.Date;

import com.kingmed.kmss.bpmn.flow.helper.ProcessHelper;
import com.kingmed.kmss.bpmn.modules.backup.model.BizApprovalStatus;
import com.kingmed.kmss.bpmn.modules.backup.model.BizApprovalTrail;
import com.kingmed.kmss.bpmn.modules.backup.service.BizApprovalStatusService;
import com.kingmed.kmss.bpmn.modules.backup.service.BizApprovalTrailService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.runtime.ProcessInstance;

import com.kingmed.kmss.bpmn.flow.service.custom.ProcessStatusSyncServiceImpl;
import com.kingmed.kmss.bpmn.flow.utils.OrgUtils;
import com.kingmed.kmss.common.constant.bpmn.BpmnConstant;
import com.kingmed.kmss.common.constant.bpmn.ProcessConstant;
import com.kingmed.kmss.common.enums.bpmn.ProcessType;
import com.kingmed.kmss.common.utils.AppContextUtil;
import com.kingmed.kmss.framework.feign.client.admin.CommonAdminFeignServiceFacade;

import lombok.extern.slf4j.Slf4j;

/**
 * 流程开始 StartExecution listener
 * 
 * 回调业务接口更新流程状态
 * @author wzg
 *
 *
 */
@Slf4j
public class ProcessStartExecutionListener implements ExecutionListener {
	
	private static final long serialVersionUID = -3036908590927665209L;
	
	BizApprovalTrailService trailSvc = AppContextUtil.getBean(BizApprovalTrailService.class);
	BizApprovalStatusService bizStatusSvc = AppContextUtil.getBean(BizApprovalStatusService.class);
	ProcessStatusSyncServiceImpl procStatusSvc = AppContextUtil.getBean(ProcessStatusSyncServiceImpl.class);
	BizApprovalStatusService approvalStatusSvc = AppContextUtil.getBean(BizApprovalStatusService.class);
	ProcessHelper processHelper = AppContextUtil.getBean(ProcessHelper.class);
	CommonAdminFeignServiceFacade remoteAdminSvc = AppContextUtil.getBean(CommonAdminFeignServiceFacade.class);

	@Override
	public void notify(DelegateExecution execution) {

		String bizType = (String) execution.getVariable(BpmnConstant.VarKeys.BIZ_TYPE);
		String bizId = (String) execution.getVariable(BpmnConstant.VarKeys.BIZ_ID);

		ProcessType processType = (ProcessType) execution.getVariable(BpmnConstant.VarKeys.PROCESS_TYPE);
		String bizKey = (String) execution.getVariable(BpmnConstant.VarKeys.BIZ_KEY );
		String bizTitle = processHelper.getProcessVariable(execution.getId(), BpmnConstant.VarKeys.BIZ_TITLE);
		String procInstId = execution.getProcessInstanceId();
		String comment = (String) execution.getVariable(BpmnConstant.VarKeys.COMMENT);
		Date requestTime = null;
	    String requestedBy = "";
		ProcessInstance procInst = processHelper.getProcessInstance(procInstId);
		if (procInst != null) {
			requestedBy = procInst.getStartUserId();
		//	requestedByDept = OrgUtils.getDeptByUser(requestedBy);
			requestTime = procInst.getStartTime();
		}
		log.debug("流程开始ExecutionListener, businessType:businessId: {}/{}",
				bizType, bizId
				);
		//保存状态表，如果流程失败，会回滚
		BizApprovalStatus status = new BizApprovalStatus();
		status.setBizKey(bizKey);
		status.setBizType(bizType);
		status.setBusinessId(bizId);
		status.setBusinessTitle(bizTitle);
		status.setNextApprover(""); 
		//status.setNextApproverDept("");
		status.setCurrStatus(ProcessConstant.Status.SUBMITTED);
		status.setProcInstId(procInstId);
		status.setRequestBy(requestedBy);
		//status.setRequestByDept(requestedByDept);
		status.setRequestTime(requestTime);
		//status.setApprovedBy(assignee);
		status.setBizType(bizType);
		bizStatusSvc.saveApprovalStatus(status);

		//保存跟踪表，如果流程失败，会回滚
		BizApprovalTrail trail = new BizApprovalTrail();
		trail.setApprovalResult(BpmnConstant.ApprovalResult.SUBMIT);
		trail.setApprovedBy(requestedBy);
		trail.setApprovedByName(remoteAdminSvc.getUserRealName(requestedBy));
		trail.setApproverDept(OrgUtils.getDeptByUser(requestedBy));
		trail.setBizKey(bizKey);
		trail.setBizType(bizType);
		trail.setBusinessId(bizId);
		trail.setComment(comment);
		trail.setProcInstId(procInstId);
		trail.setApproveTime(new Date()); 
		trail.setProcessType(processType.getCode());
		trailSvc.saveApprovalTrail(trail);

	}
	
}
